﻿using System;
using UnityEngine;

namespace GTA
{
    public class AdsDataMgr : Singleton<AdsDataMgr>
    {
        /// <summary>
		/// 广告debug类型
		/// </summary>
        public enum EAdDebugType
        {
            Interstitial,
            RewardVideo,
        }

        /// <summary>
		/// 插屏触发场景
		/// </summary>
        public enum EInterstitiaFireScene
        {
            LevelPass,
            Hold,
            RewardLevel,
            Playagain,
            /// <summary>
            /// 焦点
            /// </summary>
            Focus,      
            /// <summary>
            /// 周常冒险模式
            /// </summary>
            Adventure,
            /// <summary>
            /// 对战回合开始
            /// </summary>
            BattleRound,
            /// <summary>
            /// 重新玩minigame
            /// </summary>
            MinigameGetall,
            MiniGameStart,
            BuildExit,
            Seven
        }

        /// <summary>
		/// 激励视频触发场景
		/// </summary>
        public enum ERewardVideoFireScene
        {
            GameCoin,
            ShopSource,
        }

        public long lastSaveDataTime = 0;
        public int AdBannerViewTodayCount = 0;
        public int AdBannerViewCount = 0;
        public int AdBannerClickTodayCount = 0;
        public int AdBannerClickCount = 0;
        public int AdBannerLoadedCount = 0;
        public int AdBannerFailedCount = 0;
        public int AdBannerFailedTodayCount = 0;
        public int AdInterstitialViewTodayCount = 0;
        public int AdInterstitialViewCount = 0;
        public int AdInterstitialClickTodayCount = 0;
        public int AdInterstitialClickCount = 0;
        public int AdInterstitialCloseTodayCount = 0;
        public int AdInterstitialCloseCount = 0;
        public int AdInterstitialViewBackTodayCount = 0;
        public int AdInterstitialViewBackCount = 0;
        public int AdInterstitialFailedTodayCount = 0;
        public int AdInterstitialFailedCount = 0;
        public int AdInsterstitialMissCount = 0;
        public int AdInsterstitialTodayMissCount = 0;
        public int AdRewardVideoViewTodayCount = 0;
        public int AdRewardVideoViewCount = 0;
        public int AdRewardVideoClickTodayCount = 0;
        public int AdRewardVideoClickCount = 0;
        public int AdRewardVideoCloseTodayCount = 0;
        public int AdRewardVideoCloseCount = 0;
        public int AdRewardVideoViewBackTodayCount = 0;
        public int AdRewardVideoViewBackCount = 0;
        public int AdRewardVideoFailedTodayCount = 0;
        public int AdRewardVideoFailedCount = 0;
        public int AdRewardVideoPlayFailedTodayCount = 0;
        public int AdRewardVideoPlayFailedCount = 0;
        public int AdRewardVideoLimitTodayCount = 0;
        public int AdInterstitialWillViewTodayCount = 0;
        public int AdInterstitialWillViewCount = 0;
        public int AdRewardVideoWillViewTodayCount = 0;
        public int AdRewardVideoWillViewCount = 0;
        public bool IsShowInterstitialCloseBack = false;
        public bool IsShowRewardVideoCloseBack = false;
        public int closeReason = 0;
        public long AdInterstitialFailedTime = 0;   //插屏上一次的失败时间
        public long AdRewardVideoFailedTime = 0;    //rv上一次的失败时间
        public long AdBannerFailedTime = 0;         //banner上一次的失败时间
        public long AdInterstitialPlayTime = 0;     //插屏上一次播放时间
        public long AdRewardVideoPlayTime = 0;      //rv上一次播放时间

        public float _adValue = 0;
        public float _totalAdValue = 0;
        public long _adDateTicks;

        /// <summary>
        /// 插屏触发场景
        /// </summary>
        public string AdInterstitialFireScene;

        /// <summary>
        /// 插屏广告播放时间
        /// </summary>
        public long AdInterstitialLastShowTime;

        /// <summary>
		/// 插屏广告的渠道
		/// </summary>
        public string AdInterstitialNetwork;

        /// <summary>
		/// 插屏广告的播放ID
		/// </summary>
        public string AdInterstitialID;

        /// <summary>
        /// 插屏触发场景
        /// </summary>
        public string AdRewardVideoFireScene;

        /// <summary>
        /// 激励视频广告播放时间
        /// </summary>
        public long AdRewardVideoLastShowTime;

        /// <summary>
        /// 激励视频广告的渠道
        /// </summary>
        public string AdRewardVideoNetwork;

        /// <summary>
		/// 激励视频的播放ID
		/// </summary>
        public string AdRewardVideoID;

        public AdsDataMgr()
        {
        }

        public void Init()
        {
        }

        public void SaveData()
        {
            lastSaveDataTime = Utils.GetTimeStamp();
            DataMgr.Instance.SaveData();
        }

        /// <summary>
        /// 检测是否跨天，更新数据
        /// </summary>
        public void CheckUpdateDayData()
        {
            if (!Utils.IsSameDay(lastSaveDataTime))
            {
                AdBannerViewTodayCount = 0;
                AdBannerClickTodayCount = 0;
                AdBannerFailedTodayCount = 0;
                AdInterstitialClickTodayCount = 0;
                AdInterstitialCloseTodayCount = 0;
                AdInterstitialViewBackTodayCount = 0;
                AdInterstitialWillViewTodayCount = 0;
                AdInterstitialFailedTodayCount = 0;
                AdInsterstitialTodayMissCount = 0;
                AdRewardVideoClickTodayCount = 0;
                AdRewardVideoViewBackTodayCount = 0;
                AdRewardVideoCloseTodayCount = 0;
                AdRewardVideoViewTodayCount = 0;
                AdRewardVideoWillViewTodayCount = 0;
                AdRewardVideoFailedTodayCount = 0;
                AdRewardVideoPlayFailedTodayCount = 0;
                AdRewardVideoLimitTodayCount = 0;
            }
        }

        public void ParseData(string data)
        {
            JsonUtility.FromJsonOverwrite(data, this);
            CheckUpdateDayData();
            AdBannerLoadedCount = 0;
        }

        public string ParseToString() 
        {
            return JsonUtility.ToJson(this);
        }

        #region		打点存储数据 begin

        /// <summary>
        /// 设置广告触发场景
        /// </summary>
        /// <param name="adDebugType"></param>
        public void SetAdFireScene(EAdDebugType adDebugType, string fireScene)
        {
            switch (adDebugType)
            {
                case EAdDebugType.Interstitial:
                    AdInterstitialFireScene = fireScene;
                    break;
                case EAdDebugType.RewardVideo:
                    AdRewardVideoFireScene = fireScene;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 设置广告间隔时间
        /// </summary>
        /// <param name="adDebugType"></param>
        public void SetAdIntervalTime(EAdDebugType adDebugType, long time)
        {
            switch (adDebugType)
            {
                case EAdDebugType.Interstitial:
                    AdInterstitialLastShowTime = time;
                    break;
                case EAdDebugType.RewardVideo:
                    AdRewardVideoLastShowTime = time;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 设置广告间隔时间
        /// </summary>
        /// <param name="adDebugType"></param>
        public void SetAdNetwork(EAdDebugType adDebugType, string netwok)
        {
            switch (adDebugType)
            {
                case EAdDebugType.Interstitial:
                    AdInterstitialNetwork = netwok;
                    break;
                case EAdDebugType.RewardVideo:
                    AdRewardVideoNetwork = netwok;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 获取广告触发场景
        /// </summary>
        /// <param name="adDebugType"></param>
        /// <returns></returns>
        public string GetAdFireScene(EAdDebugType adDebugType)
        {
            switch (adDebugType)
            {
                case EAdDebugType.Interstitial:
                    return AdInterstitialFireScene;
                case EAdDebugType.RewardVideo:
                    return AdRewardVideoFireScene;
                default:
                    return AdInterstitialFireScene;
            }
        }

        /// <summary>
        /// 获取看广告到回到游戏的间隔时间
        /// </summary>
        /// <param name="adDebugType"></param>
        /// <returns></returns>
        public long GetAdIntervalTime(EAdDebugType adDebugType)
        {
            switch (adDebugType)
            {
                case EAdDebugType.Interstitial:
                    return AdInterstitialLastShowTime;
                case EAdDebugType.RewardVideo:
                    return AdRewardVideoLastShowTime;
                default:
                    return AdInterstitialLastShowTime;
            }
        }

        #endregion	打点存储数据 end
    }
}